package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.Container;
import com.meilingcloud.biobank.domain.dto.ContainerBatchDto;
import com.meilingcloud.biobank.service.IContainerService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 容器 Controller
 *
 * @author chenl
 * @date 2021-01-25
 */
@Api(value = "容器", tags = "容器")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/container")
public class ContainerController extends BaseController {

    private final IContainerService iContainerService;

    /**
     * 懒加载容器树
     */
    @ApiOperation(value = "懒加载容器树", notes = "传入父容器ID，首次传0")
    @PreAuthorize("@ss.hasPermi('biobank:container:list')")
    @GetMapping("/lazyTree/{parentId}")
    public AjaxResult lazyTree(@PathVariable("parentId") Long parentId) {
        List<Container> list = iContainerService.lazyTree(parentId);
        return AjaxResult.success(list);
    }

    /**
     * 查询容器 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:list')")
    @GetMapping("/list")
    public AjaxResult list(Container container) {
        List<Container> list = iContainerService.queryList(container);
        return AjaxResult.success(list);
    }

    /**
     * 查询叶子节点
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:list')")
    @GetMapping("/getLeaf")
    public TableDataInfo getLeaf(Container container) {
        startPage();
        List<Container> containerList = iContainerService.getLeafByParent(container.getContainerId());
        return getDataTable(containerList);
    }

    /**
     * 获取容器 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:query')")
    @GetMapping(value = "/{containerId}")
    public AjaxResult getInfo(@PathVariable("containerId") Long containerId) {
        return AjaxResult.success(iContainerService.getContainerById(containerId));
    }

    /**
     * 新增容器
     */
    @ApiOperation(value = "新增容器", notes = "新增盒子时行和列不能为0")
    @PreAuthorize("@ss.hasPermi('biobank:container:add')")
    @Log(title = "容器列表 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody Container container) {
        container.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iContainerService.saveContainer(container));
    }

    /**
     * 批量新增容器
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:add')")
    @Log(title = "容器列表 ", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@Valid @RequestBody ContainerBatchDto dto) {
        return toAjax(iContainerService.batchAddition(dto));
    }

    /**
     * 复制容器
     */
    @PreAuthorize("@ss.hasPermi('biobank:container:add')")
    @Log(title = "容器列表 ", businessType = BusinessType.OTHER)
    @PostMapping("/copy")
    public AjaxResult copy(@RequestBody Container container) {
        return toAjax(iContainerService.copyContainer(container));
    }

    /**
     * 修改容器
     */
    @ApiOperation(value = "修改容器")
    @PreAuthorize("@ss.hasPermi('biobank:container:edit')")
    @Log(title = "容器列表 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody Container container) {
        container.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iContainerService.updateContainer(container));
    }

    /**
     * 删除容器
     */
    @ApiOperation(value = "删除容器")
    @PreAuthorize("@ss.hasPermi('biobank:container:remove')")
    @Log(title = "容器列表 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{containerId}")
    public AjaxResult remove(@PathVariable("containerId") Long containerId) {
        return toAjax(iContainerService.removeContainer(containerId));
    }

    /**
     * 计算各个容器当前存储样本数并更新
     */
    @GetMapping("/calculateSampleCount")
    public AjaxResult calculateSampleCount() {
        return AjaxResult.success(iContainerService.calculateSampleCount());
    }
}
